package com.ty.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ty.entity.UserEntity;
import com.ty.mapper.UserMapper;
import com.ty.req.UserLoginReq;
import com.ty.req.UserSaveReq;
import com.ty.req.UserUpdateReq;
import com.ty.resp.CommonResp;
import com.ty.service.IUserService;
import com.ty.utils.CopyUtil;
import com.ty.utils.ShowFlake;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 *
 * @author : txy
 * @create : 2023/12/31 11:48
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, UserEntity> implements IUserService {

    @Resource
    private ShowFlake showFlake;

    @Override
    public CommonResp register(UserSaveReq req) {
        UserEntity user = CopyUtil.copy(req, UserEntity.class);
        UserEntity userDb = queryByName(req.getUser());
        CommonResp resp = new CommonResp();
        if (userDb == null) {
            user.setId(showFlake.nextId());
        } else {
            resp.setMessage("用户名称已经存在");
            resp.setSuccess(false);
            return resp;
        }
        QueryWrapper<UserEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("phone", req.getPhone()).or().eq("userEmail",req.getUserEmail());
        List<UserEntity> list = baseMapper.selectList(wrapper);
        if (!list.isEmpty()) {
            resp.setMessage("手机号或者邮箱已经被注册");
            resp.setSuccess(false);
            return resp;
        }
        int insert = baseMapper.insert(user);
        resp.setSuccess(insert > 0);
        if (insert > 0) {
            resp.setMessage("注册成功");
        } else {
            resp.setMessage("数据原因，注册失败");
        }
        return resp;
    }

    @Override
    public UserEntity login(UserLoginReq req) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("user",req.getUser());
        queryWrapper.eq("password",req.getPassword());
        UserEntity userDb = baseMapper.selectOne(queryWrapper);
        return userDb;

    }

    //查询账户是否重复
    public UserEntity queryByName(String name) {
        QueryWrapper<UserEntity> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(UserEntity::getUser, name);
        List<UserEntity> userList = baseMapper.selectList(wrapper);
        if (ObjectUtils.isEmpty(userList)) {
            return null;
        } else {
            return userList.get(0);
        }
    }

    public UserEntity queryByPhone(String phone) {
        QueryWrapper<UserEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("phone", phone);
        return baseMapper.selectOne(wrapper);
    }


    public Boolean updatePasswordByIdAndOldPassword(UserUpdateReq sysUser) {
        return baseMapper.updatePassword(sysUser) > 0;
    }

    @Override
    public Boolean updatePhone(UserUpdateReq sysUser) {
        return baseMapper.updatePhone(sysUser) > 0;
    }

    @Override
    public CommonResp getUser(Page page, String name) {
        CommonResp resp = new CommonResp();
        QueryWrapper wrapper = new QueryWrapper();
        List<UserEntity> list;
        if (StringUtils.hasText(name)) {
            wrapper.like("user", name);
        }
        list = baseMapper.selectAll(page, wrapper);
        if (!list.isEmpty()){
            list.forEach(x->{
                x.setPassword(null);
            });
        }
        resp.setContent(list);
        resp.setTotal(page.getTotal());
        return resp;
    }
}
